package cn.bxshey.configModule.ws;


import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;

import java.util.Map;

/**
 * @作者：不想说话而已
 * @Date：2024/3/8 22:00
 *
 * WebSocket 握手拦截器
 */


@Component
public class WsInterceptor extends HttpSessionHandshakeInterceptor {



    //握手前
    @Override
    public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception {

        // 打印访问地址
        System.err.println(request.getRemoteAddress().toString());


        return super.beforeHandshake(request, response, wsHandler, attributes);
    }

    //完成握手后
    @Override
    public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Exception ex) {
        // 打印访问地址
        System.err.println(request.getRemoteAddress().toString());
        super.afterHandshake(request, response, wsHandler, ex);
    }
}
